(function($) {
/*whatever jQuery stuff*/
	$(function(){	
		$(window).scroll(function() {
			if($(this).scrollTop() != 0) {
				$('#toTop').fadeIn();	
			} else {
				$('#toTop').fadeOut();
			}
		});
	 	
		$('#toTop').mouseover(function(){
			$(this).css({opacity:1});
		}).mouseout(function(){
			$(this).css({opacity:0.6});
		}).click(function() {
			$('body,html').animate({scrollTop:0},800);
		});
		
		$('*').find('input[name*="txtSearch"]').keypress(function(event){
			if(event.keyCode == 13){
				gotoSearch($(this));
				return false;
			}
		});
		if($('*').find('.btnSearch').length > 0){
			$('*').find('.btnSearch').click(function(){
				gotoSearch($(this).prev());
				return false;
			});			
		}
		
		//Hide Login Floating
		$(document).click(function(){
			$('#module_login_floating').hide();	
		});
		$('#module_login_floating').click(function(e){ e.stopPropagation(); });
		$('#module_login_floating_toggle').click(function(e){ e.stopPropagation(); $('#module_login_floating').toggle();});
		$('#f_username,#f_password,#f_rememberme').keypress(function(event){
			if(event.keyCode == 13){
				ajax_member_login();
			}
		});
		$('#module_login_floating').find('.btnLogin').click(function(){
			ajax_member_login();
		});
				
/*		$(document).click(function(){
			$('#top_login_floating').hide();	
		});
		$('#top_login_floating').click(function(e){ e.stopPropagation(); });
		$('#top_login_floating_toggle').click(function(e){ e.stopPropagation(); $('#top_login_floating').toggle();});
		$('#f_username,#f_password,#f_rememberme').keypress(function(event){
			if(event.keyCode == 13){
				ajax_member_login();
			}
		});
		$('#top_login_floating').find('.btnLogin').click(function(){
			ajax_member_login();
		});*/
		
		//Place Holder
		$('input, textarea').placeholder();	
	
		/*Rating*/
		if($('.vote').length > 0){
			$('.vote a').each(function(){
				$(this).mouseover(function(){
					var num = $(this).index();
					//Show Star
					$(this).parent().find('a:lt('+(num+1)+')').addClass('active');
					//Hide Star
					$(this).parent().find('a:gt('+num+')').removeClass('active');	
				});		
			});
		}
		
		/*Pager*/
/*		if($('.pager a').length > 0){
			$('.pager a').click(function(){
				getAjaxPager(this);
				return false;
			});		
		}*/
		
		// Open new window of Player
		if($('a._player,a._playlist').length > 0){
			$('a._player,a._playlist').click(function(){
				var url = $(this).attr('href');
				var name = $(this).attr('class');
				window.open(''+url+'',''+name +'');
				return false;
	   		});
		}
		
		//Balloon ToolTips
		if($('.balloon').length > 0){
			$(".balloon").each(function() {
				$(this).balloon({ 
					contents: $(this).next().html(), 
					position: "top",
					classname: "jquery_balloon"
		  		});
	  		});
  		}
	});
})(jQuery);

function getAjaxPager(e)
{
	var url = $(e).attr('href');
	var master = $(e).closest('*[id*="ajax_pager"]');
	master.css({"opacity":0.6});
	$.ajax({
		type:"POST",
		url: url,
		data:({
			ajax:true
		})
	}).done(function(data) {
		master.html(data);
		$('.pager a').click(function(){
			getAjaxPager(this);
			return false;
		});
		master.css({"opacity":1});
		$(document).scrollTop($('#'+master.attr('id')).offset().top - 45);
		
		$('a._player').click(function(){
			var url = $(this).attr('href');
			var name = $(this).attr('class');
			window.open(''+url+'',''+name +'');
			return false;
   		});	
	});	
	return false;	
}

function gotoSearch(e)
{
	var txtSearch = $.trim(encodeURI(e.val()));
	var module = e.attr("name").split('-');
	if(txtSearch == "") return false;
	if(module[1] == "main"){
		module = "";
		$(e).parent().find('input:checked').each(function(){
			module += $(this).attr('name')+",";
		});
		module = module.substr(0,(module.length-1));
		window.open(protocol+"://search.you2play.com/"+module+"/?text="+txtSearch+"", '_self');
	} else if(module[1] == "all") {
		window.open(protocol+"://search.you2play.com/music,movie,video,artist,album,news,chord/?text="+txtSearch+"", '_new');
	} else {
		window.open(protocol+"://search.you2play.com/"+module+"/?text="+txtSearch+"", '_new');
	}
/*	var module = "all";
	window.open(protocol+"://search.you2play.com/"+module+"/"+txtSearch+"", '_new');*/
} 
function ajax_member_login()
{
	var username = $('#f_username').val();
	var password = $('#f_password').val();
	var rememberme = ($('#f_rememberme').is(':checked') ? '1':'0');
	if(username !='' && password !=''){
			$.ajax(
				{
					url:web_path+'ajax_member_login',
					type:'post',
					data:(
						{
							username:username,
							password:password,
							rememberme:rememberme,
						}),
					dataType:'json'
				}).done(function(data)
				{
					if(data.error == 1)
					{
						alert(data.msg);
					} else
					{
						//Set Memer
						$('.memberName').html(''+data.member.username+'');
						//Show Result
						$('#module_login').hide();
						$('#module_logout').show();
						//$('#top_login_floating').hide();
						$('#module_login_floating').hide();
						//Empty Value
						//$('#top_login_floating').find('input').val('');						
						$('#module_login_floating').find('input').val('');						
					}
				});		
	}
}
/*Share Facebook*/
function fb_share(width, height) {
    var leftPosition, topPosition;
    //Allow for borders.
    leftPosition = (window.screen.width / 2) - ((width / 2) + 10);
    //Allow for title and status bars.
    topPosition = (window.screen.height / 2) - ((height / 2) + 50);
    var windowFeatures = "status=no,height=" + height + ",width=" + width + ",resizable=yes,left=" + leftPosition + ",top=" + topPosition + ",screenX=" + leftPosition + ",screenY=" + topPosition + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no";
    u=location.href;
    t=document.title;
    window.open(protocol+'://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer', windowFeatures);
    return false;
}

/*Go to Tab*/
function goTab(id,e)
{	
	$(e).closest('ul').find('a').removeClass('active');
	$(e).addClass('active');
	$(''+id+'').parent().children().hide();
	$(''+id+'').show();
}

function delete_to_playlist(e,id)
{
	$.ajax({
		type:'post',
		url:web_path+'ajax_delete_to_playlist',
		data:({
			id:id
		}),
		async:false
	}).done(function(data){
		if(id == -1){
			window.location.href = window.location.href;
		} else {
			$(e).closest('li').fadeOut('slow',function(){
				window.location.href = window.location.href;
			});			
		}
	});
}
/*Oni Select*/
function oniSelect(e)
{
	if($(e).find('.oniSelectList').is(':visible')){
		$(e).find('.oniSelectList').hide();
	} else {
		$('.oniSelectList').hide();
		$(e).find('.oniSelectList').fadeIn('fast');
	}
}
/*Ads Function*/
var currentAds; //Global currentAds
function generatTag(str) {	
	var Ads = JSON.parse(str);
	if(Ads == ""){
		var indexAt = 0;
		Ads[indexAt] = new Object();
		Ads[indexAt].ads_id = 0;
		Ads[indexAt].ads_vast = "http://ads.you2play.com/openx/delivery/fc.php";
		Ads[indexAt].ads_weight = 1;
		Ads[indexAt].limit_per_day = 0; 
	} else {
		var Sequence = "";
		for(i=0;i<Ads.length;i++){
			for(j=0;j<Ads[i].ads_weight;j++){
				Sequence += ""+i+",";
			}
		}
		Sequence = Sequence.slice(0,-1);
		var Sequence_arr = Sequence.split(',');
		var indexAt = Sequence_arr[Math.floor(Math.random()*Sequence_arr.length)];
		currentAds = Ads[indexAt];		
	}
    return Ads[indexAt].ads_vast;
}

function onTrackingEvent(event) {
   if(event != null) {
   		if(event.eventType == "start"){ //Impression
			adsStatic('Impression');
		}
		if(event.eventType == "pause"){ //Click Ads
			adsStatic('click');
		}
   }
}

function adsStatic(event)
{
	if(currentAds.ads_id > 0){
		$.ajax({
			type:'post',
			url:web_path+'ajax_ads_static',
			data:({
				ads_id:currentAds.ads_id,
				event:event
			})
		});		
	}
}
/*************************Hawkeye P&G*************************/
// Update Javascript function for Hawkeye on 6.1.2014
function myDynamicTag() {
	if (typeof ASPQ_0qHox8 !== "undefined") {
		return ASPQ_0qHox8;
	} else {
		return null;
	}
}
// Update Javascript function for Hawkeye on 6.1.2014
function mygwdTagData() {
	if (typeof gwdTagData !== "undefined") {
		return gwdTagData;
	} else {
		return null;
	}
}
/*************************End Hawkeye P&G*************************/